import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
  state: {
    isGetterRouter: false,
    isCollapse: false,
    userInfo: {},
    token: null,
    roleCodes: [],
    remenberPass: {
      loginName: '',
      password: '',
      checked: ''
    },
    routerList: [
      // {
      //   path: '/Dashboard',
      //   name: 'Dashboard',
      //   component: '/Dashboard/Dashboard.vue',
      //   code: 'Dashboard',
      //   type: 1
      // },
      // {
      //   path: '/PTList',
      //   name: 'PTList',
      //   component: '/PT-List/PTList.vue',
      //   code: 'PTList',
      //   type: 1
      // },
      // {
      //   path: '/report',
      //   name: 'Report',
      //   component: '/report/report.vue',
      //   code: 'Report',
      //   type: 1
      // },
      // {
      //   path: '/setings/settings-role',
      //   name: 'Roles setting',
      //   component: '/setings/settings-role.vue',
      //   code: 'Roles-setting',
      //   type: 1
      // },
      // {
      //   path: '/setings/roleDetail',
      //   name: 'Roles Detail',
      //   component: '/setings/updateRole.vue',
      //   code: 'Role-Detail',
      //   type: 1
      // },
      // {
      //   path: '/setings/updateRole',
      //   name: 'Update Roles',
      //   component: '/setings/updateRole.vue',
      //   code: 'Update-Role',
      //   type: 1
      // },
      // {
      //   path: '/setings/settings-account',
      //   name: 'Account setting',
      //   component: '/setings/settings-account.vue',
      //   code: 'Account-setting',
      //   type: 1
      // },
      // {
      //   path: '/setings/updateAccount',
      //   name: 'Update Account',
      //   component: '/setings/updateAccount.vue',
      //   code: 'Update-Account',
      //   type: 1
      // },
      // {
      //   path: '/setings/accountDetail',
      //   name: 'Account Detail',
      //   component: '/setings/updateAccount.vue',
      //   code: 'Account-Detail',
      //   type: 1
      // },
      // {
      //   path: '',
      //   name: '',
      //   component: '',
      //   code: 'Detele-Account',
      //   type: 2
      // },
      // {
      //   path: '/setings/createRole',
      //   name: 'Create Role',
      //   component: '/setings/createRole.vue',
      //   code: 'Create-Role',
      //   type: 1
      // },
      // {
      //   path: '/setings/createAccount',
      //   name: 'Create Account',
      //   component: '/setings/createAccount.vue',
      //   code: 'Create-Account',
      //   type: 1
      // },
      // {
      //   path: '',
      //   name: '',
      //   component: '',
      //   code: 'Detele-Role',
      //   type: 2
      // },
      // {
      //   path: '/',
      //   component: '/Dashboard/Dashboard.vue',
      //   redirect: '/Dashboard',
      //   type: 1
      // }
    ]
  },
  getters: {},
  mutations: {
    changeGetterRouter(state, value) {
      state.isGetterRouter = value
    },
    changeToken(state, value) {
      // console.log('token', value);
      state.token = value
    },
    // 控制侧边栏展开
    changeCollapse(state) {
      state.isCollapse = !state.isCollapse
    },
    remenberPassword(state, value) {
      if (value.checked) {
        state.remenberPass = {
          ...state.remenberPass,
          ...value
        }
      } else {
        state.remenberPass = { loginName: state.remenberPass.loginName, password: '', checked: '' }
      }
      // console.log('记住的密码', state.remenberPass)
    },
    changeUserInfo(state, value) {
      // 清空菜单按钮权限
      state.roleCodes = []
      state.userInfo = {
        ...state.userInfo,
        ...value
      }
      state.routerList = state.userInfo.authorizedMenuList
      state.userInfo.authorizedMenuList.forEach((item) => {
        state.roleCodes.push(item.code)
        if (item.children && item.children.length > 0) {
          item.children.forEach((itemChil) => {
            state.roleCodes.push(itemChil.code)
          })
        }
      })
      console.log('权限列表', state.roleCodes)
    },
    clearUserInfo(state, value) {
      state.userInfo = {}
    }
  },
  actions: {},
  modules: {},
  plugins: [
    createPersistedState({
      paths: [
        'isCollapse',
        'userInfo',
        'token',
        'routerList',
        'roleCodes',
        'remenberPass'
      ] // 控制是否持久化
    })
  ]
})
